package com.github.lee.unsplash.global.di

import com.github.lee.unsplash.BuildConfig
import com.github.lee.unsplash.global.interceptor.HeaderInterceptor
import com.github.lee.unsplash.global.interceptor.ResponseInterceptor
import com.github.lee.unsplash.manager.LoginManager
import com.github.lee.unsplash.model.api.AuthApi
import com.github.lee.unsplash.model.api.PhotoApi
import com.github.lee.unsplash.model.api.TopicApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
import javax.inject.Singleton

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class AuthRetrofit

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ApiRetrofit


@Module
@InstallIn(ApplicationComponent::class)
object NetworkModule {

    @ApiRetrofit
    @Singleton
    @Provides
    fun provideRetrofit(okHttp: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(okHttp)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @AuthRetrofit
    @Singleton
    @Provides
    fun provideOauthRetrofit(okHttp: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl("https://unsplash.com/")
            .client(okHttp)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    @Singleton
    @Provides
    fun provideOkHttpClient(lm: LoginManager): OkHttpClient {
        val header = mutableMapOf<String, String>()
        header["Authorization"] = "Client-ID VoF94KLbU6icKhCYEYlTnzCW0Is3yJlxHkB5jFARzIA"
        header["Accept-Version"] = "v1"
        val hi = HeaderInterceptor(lm,header)

        val builder = OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(hi)
            .addInterceptor(ResponseInterceptor())
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = when (BuildConfig.DEBUG) {
                    true -> HttpLoggingInterceptor.Level.BODY
                    else -> HttpLoggingInterceptor.Level.NONE
                }
            })
        return builder.build()
    }


    //===Desc:=========================================================

    @Singleton
    @Provides
    fun providePhotoApi(
        @ApiRetrofit
        retrofit: Retrofit
    ): PhotoApi =
        retrofit.create(PhotoApi::class.java)

    @Singleton
    @Provides
    fun provideAuthApi(
        @AuthRetrofit
        retrofit: Retrofit
    ): AuthApi =
        retrofit.create(AuthApi::class.java)


    @Singleton
    @Provides
    fun provideTopicApi(
        @ApiRetrofit
        retrofit: Retrofit
    ): TopicApi =
        retrofit.create(TopicApi::class.java)


}